﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIS.TMP.WEB.APP.Models;
using RIS.TMP.WEB.APP.AddMigrations;

namespace RIS.TMP.WEB.APP.Controllers
{
    public class MRestoreToAdminController : Controller
    {
        private AllContext db = new AllContext();

        //
        // GET: /MRestoreToAdmin/

        public ActionResult Index()
        {
            string Id = Session["LogedId"].ToString();
            var mrestoretoadmins = db.MRestoreToAdmins.Include(m => m.CAddCar).Include(m => m.CCategoryRepair).Where(x => x.RecordStatus == "U").OrderByDescending(x => x.ResId);
            return View(mrestoretoadmins.ToList());
        }

        public ActionResult IndexUser()
        {
            string Id = Session["LogedId"].ToString();
            var mrestoretoadmins = db.MRestoreToAdmins.Include(m => m.CAddCar).Include(m => m.CCategoryRepair).Where(x => x.CreateBy == Id && x.RecordStatus == "U").OrderByDescending(x => x.ResId);
            return View(mrestoretoadmins.ToList());
        }

        //
        // GET: /MRestoreToAdmin/Details/5

        public ActionResult Details(int id = 0)
        {
            MRestoreToAdmin mrestoretoadmin = db.MRestoreToAdmins.Find(id);

            db.Entry(mrestoretoadmin).State = EntityState.Modified;
            if (Session["PermissionGrop"].ToString() == "1") { mrestoretoadmin.Facstatus = 1; }
            mrestoretoadmin.UpdateBy = Session["LogedId"].ToString();
            mrestoretoadmin.UpdateWhen = DateTime.Now;
            db.SaveChanges();

            if (mrestoretoadmin == null)
            {
                return HttpNotFound();
            }
            return View(mrestoretoadmin);
        }

        //
        // GET: /MRestoreToAdmin/Create

        public ActionResult Create()
        {
            ViewBag.CCheckPlanId = new SelectList(db.CCheckPlans, "ChpId", "ChpName");
            ViewBag.AddCarId = new SelectList(db.CAddCars, "AddCarId", "AddCarlicence");
            ViewBag.CReId = new SelectList(db.CCategoryRepairs, "CReId", "CReName");
            return View();
        }

        //
        // POST: /MRestoreToAdmin/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MRestoreToAdmin mrestoretoadmin)
        {
            if (ModelState.IsValid)
            {
                db.MRestoreToAdmins.Add(mrestoretoadmin);
                mrestoretoadmin.RecordStatus = "U";
                mrestoretoadmin.CreateBy = Session["LogedId"].ToString();
                mrestoretoadmin.CreateWhen = DateTime.Now;
                mrestoretoadmin.Facstatus = 0;
                mrestoretoadmin.MRestore = 1;
                db.SaveChanges();
                if (Session["PermissionGrop"].ToString() == "1")
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("IndexUser");
                }
            }
            ViewBag.CCheckPlanId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretoadmin.CCheckPlanId);
            ViewBag.AddCarId = new SelectList(db.CAddCars, "AddCarId", "AddCarlicence", mrestoretoadmin.AddCarId);
            ViewBag.CReId = new SelectList(db.CCategoryRepairs, "CReId", "CReName", mrestoretoadmin.CReId);
            return View(mrestoretoadmin);
        }

        //
        // GET: /MRestoreToAdmin/Edit/5

        public ActionResult Edit(int id = 0, string Status = null,int IdRe = 0)
        {
            MRestoreToAdmin mrestoretoadmin = db.MRestoreToAdmins.Find(id);
            if (mrestoretoadmin == null)
            {
                return HttpNotFound();
            }

            if (id != 0 && Status == null)
            {
                db.Entry(mrestoretoadmin).State = EntityState.Modified;
                mrestoretoadmin.Facstatus = 3;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

           
            ViewBag.CCheckPlanId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretoadmin.CCheckPlanId);
            ViewBag.AddCarId = new SelectList(db.CAddCars, "AddCarId", "AddCarlicence", mrestoretoadmin.AddCarId);
            ViewBag.CReId = new SelectList(db.CCategoryRepairs, "CReId", "CReName", mrestoretoadmin.CReId);
            return View(mrestoretoadmin);
        }

        //
        // POST: /MRestoreToAdmin/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MRestoreToAdmin mrestoretoadmin)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mrestoretoadmin).State = EntityState.Modified;
                mrestoretoadmin.UpdateBy = Session["LogedId"].ToString();
                mrestoretoadmin.Facstatus = 0;
                mrestoretoadmin.UpdateWhen = DateTime.Now;
                db.SaveChanges();
                if (Session["PermissionGrop"].ToString() == "1")
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return RedirectToAction("IndexUser");
                }
            }

            ViewBag.CCheckPlanId = new SelectList(db.CCheckPlans, "ChpId", "ChpName", mrestoretoadmin.CCheckPlanId);
            ViewBag.AddCarId = new SelectList(db.CAddCars, "AddCarId", "AddCarlicence", mrestoretoadmin.AddCarId);
            ViewBag.CReId = new SelectList(db.CCategoryRepairs, "CReId", "CReName", mrestoretoadmin.CReId);
            return View(mrestoretoadmin);
        }

        //
        // GET: /MRestoreToAdmin/Delete/5

        public ActionResult Delete(int id = 0)
        {
            MRestoreToAdmin mrestoretoadmin = db.MRestoreToAdmins.Find(id);
            if (mrestoretoadmin == null)
            {
                return HttpNotFound();
            }
            return View(mrestoretoadmin);
        }

        //
        // POST: /MRestoreToAdmin/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MRestoreToAdmin mrestoretoadmin = db.MRestoreToAdmins.Find(id);
            db.Entry(mrestoretoadmin).State = EntityState.Modified;
            mrestoretoadmin.RecordStatus = "D";
            mrestoretoadmin.UpdateBy = Session["LogedId"].ToString();
            mrestoretoadmin.UpdateWhen = DateTime.Now;
            //db.MRestoreToAdmins.Remove(mrestoretoadmin);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}